package com.hg.api;

import java.util.HashMap;
import java.util.Map;

public class WeixinApiService extends ApiService
{
	public WeixinApiService(Map root)
	{
		super(root);
	}

	/**
	 * 获取二维码
	 * 二维码路径:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=
	 * @param scene_id
	 * @return
	 */
	public String getQrcodeTicket(int scene_id){
		return this.getQrcodeTicket(scene_id, 2592000);//30天过期
	}
	
	/**
	 * 二维码路径:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=
	 * @param scene_id 	场景ID
	 * @param expire	过期时间
	 * @return
	 */
	public String getQrcodeTicket(int scene_id,int expire){
		String ticket = null;
		if(scene_id>0){
			String url = weixinApi_url+"qrcode/create.do";
			url = url+"?scene_id="+scene_id+"&expire="+expire;	
			String return_json = hxc.get(url);
			if(isSuccess(return_json)){
				ticket = getString(return_json, "ticket");
			}
		}
		
		return ticket;
	}
	
	/**
	 * 支付并下单
	 * @param root		依赖于Action中的 Map root
	 * @param user_id
	 * @param payment_id
	 * @param memo
	 * @param money
	 * @param ecv_id
	 * @param order_type
	 * @param notify_url
	 * @param return_url
	 */
	public void weixinPay(Map root,int order_id,int user_id,int payment_id,String memo,double money
			,int ecv_id,int order_type,String notice_sn,String notify_url,String return_url){
		Map<String, String> params = new HashMap<String, String>();
		params.put("order_id", order_id+"");
		params.put("user_id", user_id+"");
		params.put("payment_id", payment_id+"");
		params.put("memo", memo);
		params.put("money", money+"");
		params.put("ecv_id", ecv_id+"");
		params.put("order_type", order_type+"");
		params.put("notice_sn", notice_sn);
		params.put("notify_url",notify_url);
		params.put("return_url", return_url);
		String jump_url = weixin_pay_api_url+"/cgi-bin/pay/unifiedorder.do";
		String return_json=hxc.post(jump_url, params);
		if(isSuccess(return_json)){
			js_redirect(root, getString(return_json, "pay_url"));
		}else{
			json_return(root, getCodeAndMsg(return_json));
		}
	}
}
